/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.business.bean.NoticBean;
import com.rf.business.dao.NoticeDepMapper;
import com.rf.business.model.Notice;
import com.rf.business.model.NoticeDep;
import com.rf.business.model.NoticeDepExample;
import com.rf.business.service.NoticeDepService;
import com.rf.business.service.NoticeService;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.DepUser;
import com.rf.sys.model.Department;
import com.rf.sys.model.User;
import com.rf.sys.service.DepartmentService;
import com.rf.sys.service.UserService;
import com.rf.sys.utils.PageUtils;

@Service("noticeDepService")
public class NoticeDepServiceImpl implements NoticeDepService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private NoticeDepMapper noticeDepMapper;//注入dao

	@Resource
	private DepartmentService departmentService;
	@Resource
	private NoticeService noticeService;
	@Resource
	private UserService userService;
   	/**
	 * 根据id获取数据
	 * @param noticeDepId
	 * @return NoticeDep
	 */
   	@Override
	public NoticeDep getNoticeDepByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return noticeDepMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据NoticeDep获取数据
	 * @param NoticeDep
	 * @return List<NoticeDep>
	 */
	@Override
	public List<NoticeDep> getNoticeDep(NoticeDep noticeDep) throws Exception{
		// TODO Auto-generated method stub
		NoticeDepExample example = new NoticeDepExample();
		com.rf.business.model.NoticeDepExample.Criteria criteria= example.createCriteria();
		if(noticeDep.getnId() != null && !noticeDep.getnId().equals("")){
			criteria.andNIdEqualTo(noticeDep.getnId());
		}
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<NoticeDep> list = noticeDepMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<NoticeDep>
	 */
	@Override
	public List<NoticeDep> getNoticeDep() throws Exception{
		// TODO Auto-generated method stub
		NoticeDepExample example = new NoticeDepExample();
		com.rf.business.model.NoticeDepExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return noticeDepMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param NoticeDep
	 * @return
	 */
	@Override
	public void addNoticeDep(NoticeDep noticeDep) throws Exception{
		// TODO Auto-generated method stub
		noticeDepMapper.insert(noticeDep);
	}

	/**
	 * 修改数据
	 * @param NoticeDep
	 * @param
	 */
	@Override
	public void updateNoticeDep(NoticeDep noticeDep) throws Exception{
		// TODO Auto-generated method stub
		noticeDepMapper.updateByPrimaryKeySelective(noticeDep);
	}
	
	/**
	 * 删除数据
	 * @param noticeDepId
	 * @param
	 */
	@Override
	public void deleteNoticeDep(String id
) throws Exception{
		// TODO Auto-generated method stub
		noticeDepMapper.deleteByPrimaryKey(id
);
	}

	/**
	 * 获取数据分页
	 * @param NoticeDep
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getNoticeDepPage(NoticeDep noticeDep,PageUtils pageUtils) throws Exception{
		NoticeDepExample example = new NoticeDepExample();
		com.rf.business.model.NoticeDepExample.Criteria criteria= example.createCriteria();
		Session session = SecurityUtils.getSubject().getSession();

		boolean isAdmin = (Boolean)session.getAttribute("isAdmin");
		if(!isAdmin){
			criteria.andDepIdEqualTo((String)session.getAttribute("depSessionId"));
		}
		if(noticeDep.getId() != null && !noticeDep.getId().equals("")){
			criteria.andIdEqualTo(noticeDep.getId());
		}
		if(noticeDep.getIsRead() != null && !noticeDep.equals("")){
			criteria.andIsReadEqualTo(noticeDep.getIsRead());
		}
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<NoticeDep> list = noticeDepMapper.selectByExample(example);
	
		List<NoticBean> listBean =  new ArrayList<NoticBean>();
		if(list != null && list.size() > 0){
			for(NoticeDep n:list){
				NoticBean nB = new NoticBean();
				Notice n1 = noticeService.getNoticeByID(n.getnId());
				if(n1 != null){
					Department dep1 = departmentService.getDepartmentByID(n.getDepId());
					nB.setId(n.getId());
					nB.setTitel(n1.getTitel());
					nB.setContent(n1.getContent());
					nB.setCreateTime(n1.getCreateTime());
					User user = userService.getUserByID(n1.getCreateUser());
					nB.setUserName(user.getUserName());
					nB.setDepName(dep1.getName());
					nB.setIsRead(n.getIsRead());
					listBean.add(nB);
				}
			}
		}
		ListSort(listBean);  
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", listBean);
		return dateMap;
	}
	private static void ListSort(List<NoticBean> list) {
		         Collections.sort(list, new Comparator<NoticBean>() {
		             @Override
		             public int compare(NoticBean o1, NoticBean o2) {
		                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		                 try {
		                     Date dt1 = o1.getCreateTime();
		                     Date dt2 = o2.getCreateTime();
		                     if (dt1.getTime() > dt2.getTime()) {
		                         return -1;
		                     } else if (dt1.getTime() < dt2.getTime()) {
		                         return 1;
		                     } else {
		                         return 0;
		                     }
		                 } catch (Exception e) {
		                     e.printStackTrace();
		                 }
		                 return 0;
		             }
		         });
		     }    
	      
}

